"""This module requires lxml and nltk!"""

from lxml.cssselect import CSSSelector
import lxml.html
import nltk
import re
import urllib2
import random

content = []
irc = None

def init(bot):
    global irc
    irc = bot
    irc.cmd.events['PRIVMSG'].subscribe(privmsg)

def privmsg(sender, args):
    global irc

    msg = args[0]

    if 'say something' in msg.lower():
        text = generate()

        stok = nltk.tokenize.punkt.PunktSentenceTokenizer()
        sentences = stok.tokenize(text)
        sentence = max(sentences, key=len)
        irc.privmsg(args[2], sentence.encode('ascii', 'replace'))

def fetch_page():
    url = urllib2.urlopen('http://bash.org/?random1')
    html = url.read()
    url.close()

    return html

def parse_html(html):
    root = lxml.html.fromstring(html)
    sel = CSSSelector('p.qt')
    quotes = sel(root)

    tokenizer = nltk.tokenize.regexp.WordPunctTokenizer()

    for quote in quotes:
        text = quote.text_content()
        for line in text.split('\n'):
            pieces = line.split(' ')
            sentence = ' '.join(pieces[1:])
            if sentence[-1:] not in '.,!?':
                sentence = sentence + '.'
            words = tokenizer.tokenize(sentence)
            for word in words:
                content.append(word)

def generate():
    for i in xrange(0, 4):
        html = fetch_page()
        parse_html(html)

    model = nltk.model.ngram.NgramModel(3, content)
    starting = model.generate(100)[-2:]
    #out = ' '.join(model.generate(100, starting))
    #out = re.sub(' ?(\\.|,|!|\\?|:|;|\\(|\\)|\'|") ?', '\\1', out)
    #out = re.sub('(\\.|,|!|\\?|:|;)([a-zA-Z0-9])', '\\1 \\2', out)
    words  = model.generate(120, starting)
    out = fix_punct(words)
    
    return out

def fix_punct(content):
    output = ''
    punct = """,.?!;":'()`"""
    last = ''
    for w in content:
        if last and last not in punct:
            if w not in ',.?!\'`)' or w == '(':
                output += ' '
        if last and last in punct and w in punct:
            pass
        elif last and last in punct and w not in punct:
            if last in '.?!;,:)':
                output += ' '
                if last in '.?!':
                    w = w.capitalize()
        if not output:
            w = w.capitalize()
        output += w
        last = w

    output = output.replace(" - ", "-") + '.'
    
    w = output.split()
    if "re" == w[0].lower():
        w[0] = random.choice(["They're", "We're", "You're"])
    elif "ll" == w[0].lower():
        w[0] = random.choice(["I'll", "We'll", "They'll", "You'll"])
    elif "m" == w[0].lower():
        w[0] = "I'm"
    elif "t" == w[0].lower():
        w[0] = random.choice(["Can't", "Didn't", "Don't", "Isn't"])
    elif "ve" == w[0].lower():
        w[0] = random.choice(["I've", "We've", "You've", "They've"])
    elif "s" == w[0].lower():
        w[0] = random.choice(["It's", "That's"])
    elif w[0][0] in ",.!?'":
        x = w.pop(0)
        while x[0] in ",.!?'":
            x = w.pop(0)

    w[0] = w[0].capitalize()
    return " ".join(w)
